package com.maixin.code.dao;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;

import com.maixin.code.entity.Menus;

public class MenusDAO extends BaseDAO<Menus> {
	
	/**
	 * 获得所有的菜单集合数据
	 * @param start
	 * @param limit
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Menus> findAllMenus(Integer start,Integer limit){
		try {
			String sql = "select a.id id ,a.menuname text,a.menucode menucode ,a.url url,b.menuname parentname ,a.leaf leaf from menus a left join menus b on a.parentid = b.id order by a.id desc";
			SQLQuery query = (SQLQuery) super.getSession().createSQLQuery(sql).
			setFirstResult(start).setMaxResults(limit).setResultTransformer(Transformers.aliasToBean(Menus.class));
			query.addScalar("id", Hibernate.INTEGER);
			query.addScalar("text", Hibernate.STRING);
			query.addScalar("menucode", Hibernate.STRING);
			query.addScalar("url", Hibernate.STRING);
			query.addScalar("parentname", Hibernate.STRING);
			query.addScalar("leaf", Hibernate.STRING);
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 升序排列菜单
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Menus> findAllMenus(){
		String sql = "from Menus s order by s.id asc";
		Query query = super.getSession().createQuery(sql);
		return query.list();
	}
	
}
